using System;
using System.Drawing;

namespace Meta.Basic2D {
	public struct Interval {
		public float min, max;

		public Interval(float min, float max) {
			if (max<min) {
				float temp=max;
				max = min;
				min = temp;
			}

			this.min = min;
			this.max = max;
		}

		public void Union(Interval i) {
			Extend(i.min, i.max);
		}

		public float Size {
			get {
				return max-min;
			}
		}

		public void ExtendProjection(PointF a, PointF b) {
			float dot = a.X*b.X+a.Y*b.Y;

			if (dot<min)
				min = dot;
			else if (dot>max)
				max = dot;
		}

		// This shouldn't really be in this class
		public static float Project(PointF a, PointF b) {
			return a.X*b.X+a.Y*b.Y;
		}

		public static Interval ProjectionInterval(PointF a, PointF b) {
			float dot = a.X*b.X+a.Y*b.Y;

			return new Interval(dot, dot);
		}
	
		public void ExtendMin(float newMin) {
			if (newMin<min)
				min=newMin;
		}
		public void ExtendMax(float newMax) {
			if (newMax>max)
				max=newMax;
		}

		public void Extend(float newMin, float newMax) {
			if (newMax<newMin) {
				float temp = newMax;
				newMax=newMin;
				newMin = temp;
			}
			if (newMin<min)
				min=newMin;
			if (newMax>max)
				max=newMax;
		}
	}
}
